package com.yq.design.observer;

import java.util.ArrayList;

/**
 * @ClassName WeatherData
 * @Description TODO
 * @Author yaoqiang
 * @Date 2021/8/26 10:48
 * @Version 1.0
 */
public class WeatherData implements Subject {
    private int tempurature;
    //观察者集合
    private ArrayList<Observer> observers;

    public WeatherData() {
        observers=new ArrayList<Observer>();
    }

    public void changeData(int tempurature){
        this.tempurature=tempurature;
        notifyObservers();
    }

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }


    @Override
    public void notifyObservers() {
        for (int i=0;i<observers.size();i++){
            observers.get(i).update(this.tempurature);
        }
    }
}
